package com.yehaikeji.workflow.controller.management;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.util.StringUtil;
import com.yehaikeji.workflow.bean.unit;
import com.yehaikeji.workflow.controller.bean.ResultMessage;
import com.yehaikeji.workflow.service.unitService;

import io.swagger.annotations.ApiOperation;

@RequestMapping("untilManager")
@Controller
public class unitlController {
	@Autowired
	unitService unitService;
	
	@RequestMapping(value="insertunit",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="增加部门信息",notes="type=1为社区，type=2办理部门或者中转部门")
	private ResultMessage insertunit(unit unit) {
		ResultMessage resultMessage=new ResultMessage();
		try {
			if(StringUtil.isEmpty(unit.getName())||unit.getType()==null||unit.getType()<=0||unit.getType()>2) {
				resultMessage.setMessage("参数为空或type错误");
			    resultMessage.setSuccess(false);
			}else {
				unit.setUnitid(unitService.insertunit(unit));
				resultMessage.setData(unit);
			    resultMessage.setSuccess(true);
			}
		    
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setSuccess(false);
			resultMessage.setMessage(e.getMessage());
		}
		return  resultMessage;
	}
	
	
	
	@RequestMapping(value="getunitlist",method=RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="返回所有部门信息")
	private ResultMessage selectallunit(Integer type) {
		ResultMessage resultMessage=new ResultMessage();
		try {
		    if(type==null||type<=0||type>2) {
		    	throw new RuntimeException("参数为空或错误");
		    }
		    resultMessage.setData(unitService.selectunitall(type));
		    resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setSuccess(false);
			resultMessage.setMessage(e.getMessage());
		}
		return  resultMessage;
	}
	
	@RequestMapping(value="delectunit",method=RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="删除一个部门信息")
	private ResultMessage deleteunit(String unitid) {
		ResultMessage resultMessage=new ResultMessage();
		try {
		    resultMessage.setData(unitService.deleteunit(unitid));
		    resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setSuccess(false);
			resultMessage.setMessage(e.getMessage());
		}
		return  resultMessage;
	}
	
	
}
